const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const redirectToPage = (url,obj) =>{
  obj.setData({
    loadingHidden: false
  })
  wx.navigateTo({
    url: url,
    complete(){
      setTimeout(function () {
        // 延迟 200 ms 关闭遮罩
        obj.setData({
          loadingHidden: true
        })
      }, 200)
    }
  })
}

const redirectToPageAndDistroy = (url,obj) =>{
  obj.setData({
    loadingHidden: false
  })
  wx.redirectTo({
    url: url,
    complete(){
      setTimeout(function () {
        // 延迟 200 ms 关闭遮罩
        obj.setData({
          loadingHidden: true
        })
      }, 200)
    }
  })
}

const toast = (msg,type = 'none', mask = true) => {
  wx.showToast({
    title: msg,
    icon: type,
    mask: mask
  })
}

const mask= (obj) =>{
  obj.setData({
    loadingHidden: false
  })
}

const removeMask= (obj, time=0) =>{
  setTimeout(function () {
    obj.setData({
      loadingHidden: true
    })
  }, time)
}



const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  redirectToPage: redirectToPage,
  mask: mask,
  removeMask: removeMask,
  toast: toast,
  redirectToPageAndDistroy: redirectToPageAndDistroy
}
